package com.zuosh.netty5;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * Created by kai on 2015/12/25 0025.
 */
public class NettyIdleStatsTest {
    public static void main(String[] args) throws InterruptedException {
        //sever
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class);
            b.option(ChannelOption.SO_BACKLOG.SO_KEEPALIVE, true);
            b.option(ChannelOption.SO_BACKLOG, 128);

            b.handler(new LoggingHandler(LogLevel.DEBUG));
            b.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ChannelPipeline pipeline = ch.pipeline();
                    //add handler
                    pipeline.addLast("idlestathandler", new IdleStateHandler(5, 4, 4));
                    //add idle handler
                    pipeline.addLast(new ChannelHandlerAdapter() {
                        @Override
                        public void channelActive(ChannelHandlerContext ctx) throws Exception {
                            System.out.println("channelCome" + ctx.channel().id());
                        }

                        @Override
                        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
                            if (IdleStateEvent.class.isAssignableFrom(evt.getClass())) {
                                IdleStateEvent event = (IdleStateEvent) evt;
                                if (event.state() == IdleState.READER_IDLE) {
                                    System.out.println("readIdle");
                                } else if (event.state() == IdleState.WRITER_IDLE) {
                                    System.out.println("write idle");
                                    ctx.writeAndFlush(4);
                                } else if (event.state() == IdleState.ALL_IDLE) {
                                    System.out.println("all idle");
                                } else {
                                    System.out.println(" i don not know...");
                                }
                            }
                        }
                    });
                }
            });

            //start server
            ChannelFuture channelFuture = b.bind(8080).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
            System.out.println(" shut down gracefully  already...");
        }
    }
}
